package com.liwenfeng.bigevent.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liwenfeng.bigevent.mapper.ArticleMapper;
import com.liwenfeng.bigevent.pojo.Article;
import com.liwenfeng.bigevent.pojo.PageBean;
import com.liwenfeng.bigevent.service.ArticleService;
import com.liwenfeng.bigevent.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;


    @Override
    public void add(Article article) {
        article.setCreateUser(((Map<String, Integer>) ThreadLocalUtil.get()).get("id"));
        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, String categoryId, String state) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Article> page = articleMapper.list(((Map<String, Integer>) ThreadLocalUtil.get()).get("id"), categoryId, state);
        PageBean<Article> pageBean = new PageBean<>();
        pageBean.setItems(page.getResult());
        pageBean.setTotal(page.getTotal());
        return pageBean;
    }

    @Override
    public void update(Article article) {

        articleMapper.update(article);
    }

    @Override
    public Article getById(Integer id) {
        return articleMapper.getById(id);
    }

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }
}
